using System;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using Serilog;

// WPF 相关（使用别名）
using WPF = System.Windows;
using WPFControls = System.Windows.Controls;

namespace PC
{
    public partial class ApiTestWindow : WPF.Window
    {
        private readonly ILogger _logger;
        private readonly HttpClient _httpClient;
        private readonly string _baseUrl = "http://localhost:3003";

        public ApiTestWindow()
        {
            InitializeComponent();
            _logger = Log.Logger;
            _httpClient = new HttpClient();
        }

        private async void UploadButton_Click(object sender, WPF.RoutedEventArgs e)
        {
            try
            {
                var openFileDialog = new Microsoft.Win32.OpenFileDialog();
                if (openFileDialog.ShowDialog() == true)
                {
                    var fileInfo = new FileInfo(openFileDialog.FileName);
                    _logger.Debug("[文件] 文件信息: 名称={Name}, 大小={Size}字节", fileInfo.Name, fileInfo.Length);

                    var content = new MultipartFormDataContent();
                    var fileContent = new StreamContent(File.OpenRead(openFileDialog.FileName));
                    fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
                    content.Add(fileContent, "file", fileInfo.Name);

                    var response = await _httpClient.PostAsync($"{_baseUrl}/upload", content);
                    var responseContent = await response.Content.ReadAsStringAsync();
                    _logger.Debug("[文件] 上传响应: 状态码=\"{Status}\", 内容={Content}", response.StatusCode, responseContent);

                    if (response.IsSuccessStatusCode)
                    {
                        WPF.MessageBox.Show("文件上传成功！", "提示", WPF.MessageBoxButton.OK, WPF.MessageBoxImage.Information);
                    }
                    else
                    {
                        throw new Exception($"上传失败：{response.StatusCode}");
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "[文件] 上传文件失败");
                WPF.MessageBox.Show("上传文件失败：" + ex.Message, "错误", WPF.MessageBoxButton.OK, WPF.MessageBoxImage.Error);
            }
        }
    }
} 